const express = require('express');
const app = express();
const mongo = require('../dataBase/users');
const router = express.Router();
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
router.post('/', jsonParser, (req, res) => {
    mongo.Users.find({ userId: req.body.userId }, (err, docu) => {
        if (err) {
            res.send({ status: false });
            return
        }
        // console.log(1);
        return docu
    }).then((docu) => {
        // console.log(docu.length);
        if (docu.length == 0) {
            var user = {
                userId: req.body.userId,
                password: req.body.password,
                name: req.body.name,
                avatarImg: req.body.serverIp + '/static' + '/image' + '/' + req.body.userId + '.' + req.body.imgType
            }
            console.log(user.avatarImg);
            new mongo.Users(user).save((err) => {
                if (err) throw err
                res.send({ status: true })
            })
        } else {
            res.send({ status: false });
        }
    })
})

module.exports = router;